﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FilmBookingTicket.Models;

namespace FilmBookingTicket.Controllers
{
    public class ChairController : Controller
    {
        private FilmTicketBookingSystemEntities db = new FilmTicketBookingSystemEntities();

        //
        // GET: /Chair/

        public ActionResult Index()
        {
            var chairs = db.Chairs.Include(c => c.ChairType).Include(c => c.Room);
            return View(chairs.ToList());
        }

        //
        // GET: /Chair/Details/5

        public ActionResult Details(int id = 0)
        {
            Chair chair = db.Chairs.Find(id);
            if (chair == null)
            {
                return HttpNotFound();
            }
            return View(chair);
        }

        //
        // GET: /Chair/Create

        public ActionResult Create()
        {
            ViewBag.ChairTypeId = new SelectList(db.ChairTypes, "ChairTypeId", "TypeName");
            ViewBag.RoomId = new SelectList(db.Rooms, "RoomId", "RoomName");
            return View();
        }

        //
        // POST: /Chair/Create

        [HttpPost]
        public ActionResult Create(Chair chair)
        {
            if (ModelState.IsValid)
            {
                db.Chairs.Add(chair);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ChairTypeId = new SelectList(db.ChairTypes, "ChairTypeId", "TypeName", chair.ChairTypeId);
            ViewBag.RoomId = new SelectList(db.Rooms, "RoomId", "RoomName", chair.RoomId);
            return View(chair);
        }

        //
        // GET: /Chair/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Chair chair = db.Chairs.Find(id);
            if (chair == null)
            {
                return HttpNotFound();
            }
            ViewBag.ChairTypeId = new SelectList(db.ChairTypes, "ChairTypeId", "TypeName", chair.ChairTypeId);
            ViewBag.RoomId = new SelectList(db.Rooms, "RoomId", "RoomName", chair.RoomId);
            return View(chair);
        }

        //
        // POST: /Chair/Edit/5

        [HttpPost]
        public ActionResult Edit(Chair chair)
        {
            if (ModelState.IsValid)
            {
                db.Entry(chair).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ChairTypeId = new SelectList(db.ChairTypes, "ChairTypeId", "TypeName", chair.ChairTypeId);
            ViewBag.RoomId = new SelectList(db.Rooms, "RoomId", "RoomName", chair.RoomId);
            return View(chair);
        }

        //
        // GET: /Chair/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Chair chair = db.Chairs.Find(id);
            if (chair == null)
            {
                return HttpNotFound();
            }
            return View(chair);
        }

        //
        // POST: /Chair/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Chair chair = db.Chairs.Find(id);
            db.Chairs.Remove(chair);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}